package com.leetcode.LC;

import java.util.HashMap;
import java.util.Map;

public class LC383 {
    public boolean canConstruct(String ransomNote, String magazine) {
        //另一种思路数组标记法，magazine出现的字符+1，ransomNote出现的字符-1，最后检测有没有负数
        if(magazine.length()<ransomNote.length()){
            return false;
        }

        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < magazine.length(); i++) {
            if (map.containsKey(magazine.charAt(i))){
                map.put(magazine.charAt(i),map.get(magazine.charAt(i))+1);
            }else {
                map.put(magazine.charAt(i),1);
            }
        }

        for (int i = 0; i < ransomNote.length(); i++) {
            if (map.containsKey(ransomNote.charAt(i))){
                if (map.get(ransomNote.charAt(i))-1<0){
                    return false;
                }
                map.put(ransomNote.charAt(i),map.get(ransomNote.charAt(i))-1);
            }else {
                return false;
            }
        }

        return true;
    }

    public static void main(String[] args) {
        System.out.println(new LC383().canConstruct("aa", "ab"));
    }
}
